package com.neuedu.jdbc.dao;

import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 项目名称:  MyJDBC 
 * 包：  com.neuedu.jdbc.dao 
 * date:   2025/11/7   11:30  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class UserDaoBak {

    /**
     * 根据主键查询单条结果
     * @param id
     * @return
     */
    public Map<String,Object> selectById(long id){


        // String url = "http://www.baidu.com";
        String url = "jdbc:mysql://localhost:3306/his?useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "root";


        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        Map obj = null;

        // 旧版  5.xx-
        // Class.forName("com.mysql.jdbc.Driver");
        // 8.x +
        try {
            // 1 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");


            // 2 创建连接
            conn = DriverManager.getConnection(url, user, password);

            // 3 创建语句块stmt
            stmt = conn.createStatement();

            // 4 执行sql 语句
            String sql = "select  `id`,  `username`, `password`,  `nickname`,  `last_login`, " +
                    " `status`,  `create_time`, `create_user`,  `update_time`, `update_user`   from user where id = "+id   ;


            rs = stmt.executeQuery(sql);


            // 解析结果集
            if (rs.next()) {

                obj = new HashMap();

                int index = 0;
                long userId = rs.getLong(++index);
                String username = rs.getString(++index);
                String pd = rs.getString(++index);
                String nickname = rs.getString(++index);
                Date lastLogin = rs.getTimestamp(++index);
                String status = rs.getString(++index);
                String createtime = rs.getString(++index);
                Long createuser = rs.getLong(++index);
                Date updateTime = rs.getTimestamp(++index);
                Long updateuser = rs.getLong(++index);

                obj.put("id",userId);
                obj.put("username",username);
                obj.put("pd",pd);
                obj.put("nickname",nickname);
                obj.put("lastLogin",lastLogin);
                obj.put("status",status);
                obj.put("createtime",createtime);
                obj.put("createuser",createuser);
                obj.put("updateTime",updateTime);
                obj.put("updateuser",updateuser);


            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }


            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }


        return obj;
    }



    /**
     * 根据条件查询集合
     * @param
     * @return
     */
    public List selectList(String username ,String nickname){
        return null;
    }


    public int   updateById(long id , String username,String password){
        return 0;
    }

    public int   insert(long id , String username,String password){
        return 0;
    }

    public int   deleteById(long id){
        return 0;
    }


    public static void main(String[] args) {

        UserDaoBak userDao = new UserDaoBak();


        //根据主键查询一条
        long id = 2;
        Map<String, Object> user = userDao.selectById(id);
        System.out.println(user);
        //
        String username  = (String) user.get("username");
        String password  = (String) user.get("password");

        System.out.println("username = " + username);
        System.out.println("password = " + password);


    }
}
